﻿using Microsoft.Extensions.Options;
using RabbitMQ.Client;  
using SHKF.B2C.MQBase.Model;
using System;

namespace SHKF.B2C.MQBase
{
    public class RabbitMqCore
    {
        //订单推送 {0}:平台
        private string OrderPush_Queue = "SHKF.B2C.OrderPush.{0}.Queue";
        private string OrderPush_Exchange = "SHKF.B2C.OrderPush.{0}.Exchange";
        private string OrderPush_RoutingKey = "SHKF.B2C.OrderPush.{0}.RoutingKey";

        //更新库存, 各平台订单回调更新B2C库存后通知Erp更新库存，Erp通知B2C更新库存

        /// <summary>
        /// 更新库存队列
        /// </summary>
        public string Update_Stock_Queue = "SHKF.B2C.UpdateSock.Queue";

        /// <summary>
        /// 更新库存交换机
        /// </summary>
        public string Update_Stock_Exchange = "SHKF.B2C.UpdateSock.Exchange";

        //public string Update_Stock_B2C_RoutingKey_Erp = "SHKF.B2C.UpdateSock.RoutingErp";
        //public string Update_Stock_B2C_RoutingKey_Plat = "SHKF.B2C.UpdateSock.RoutingPlat";

        //B2C通知各平台更新库存 广播模式 {0}:平台
        private string Update_Stock_Plat_Queue = "SHKF.B2C.UpdateSock.Plat.{0}.Queue";
        private string Update_Stock_Plat_Exchange = "SHKF.B2C.UpdateSock.Plat.Exchange";
          
        protected readonly MQConfig　config;
        public IConnection connection;

        public RabbitMqCore(IOptionsMonitor<MQConfig> config)
        {
            this.config = config.CurrentValue;

            var factory = new ConnectionFactory
            {
                HostName = this.config.HostName, //ip
                Port = this.config.Port, // 端口
                UserName = this.config.UserName, // 账户
                Password = this.config.Password, // 密码
                VirtualHost = "/"   // 虚拟主机
            };

            connection = factory.CreateConnection();
        }
    
        protected byte[] CreateBody(string message)
        {
            var body = System.Text.Encoding.UTF8.GetBytes(message);
            return body;
        }

        #region 双工

        public IModel CreateOrderChannel()
        {
            IModel model = connection.CreateModel();

            string queue = this.GetOrderPushQueue();
            string exchange = this.GetOrderPushExchange();
            string key = this.GetOrderPushRoutingKey();

            //队列  
            model.QueueDeclare(queue: queue, durable: true, exclusive: false, autoDelete: false, arguments: null);
            model.ExchangeDeclare(exchange: exchange, ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
            model.QueueBind(queue: queue, exchange: exchange, routingKey: key, arguments: null);

            return model;
        }

        public string GetOrderPushQueue()
        {
            return string.Format(OrderPush_Queue, this.config.Platform);
        } 
        protected string GetOrderPushExchange()
        {
            return string.Format(OrderPush_Exchange, this.config.Platform);
        } 
        protected string GetOrderPushRoutingKey()
        {
            return string.Format(OrderPush_RoutingKey, this.config.Platform);
        }

        #endregion

        //public string GetUpdateStockB2CQueue()
        //{
        //    return string.Format(Update_Stock_Queue, this.config.Platform);
        //}
        //protected string GetUpdateStockB2CExchange()
        //{
        //    return string.Format(Update_Stock_B2C_Exchange, this.config.Platform);
        //}
         
        public string GetUpdateStockPlaQueue()
        {
            return string.Format(Update_Stock_Plat_Queue, this.config.Platform);
        } 
        protected string GetUpdateStockPlaExchange()
        {
            return string.Format(Update_Stock_Plat_Exchange, this.config.Platform);
        }

    }
}
